package com.tourism.controller;

import com.tourism.common.Result;
import com.tourism.entity.ScenicRating;
import com.tourism.service.ScenicRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;

@RestController
@RequestMapping("/api/admin/scenicRating")
@CrossOrigin
public class ScenicRatingController {

    @Autowired
    private ScenicRatingService scenicRatingService;

    @GetMapping("/{id}")
    public Result getRatingById(@PathVariable Integer id) {
        return Result.success(scenicRatingService.getById(id));
    }

    @GetMapping("/list")
    public Result getRatingList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer spotId,
            @RequestParam(required = false) String userName) {
        return scenicRatingService.getRatingList(page, pageSize, spotId, userName);
    }

    @PostMapping("/add")
    public Result addRating(@RequestBody ScenicRating scenicRating) {
        scenicRating.setRatingTime(new Date());
        return scenicRatingService.addRating(scenicRating);
    }

    @PutMapping("/update")
    public Result updateRating(@RequestBody ScenicRating scenicRating) {
        if (scenicRating.getId() == null) {
            return Result.fail("ID不能为空");
        }
        return scenicRatingService.updateRating(scenicRating);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteRating(@PathVariable Integer id) {
        return scenicRatingService.deleteRating(id);
    }

    @PostMapping("/batchDelete")
    public Result batchDeleteRatings(@RequestBody List<Integer> ids) {
        return scenicRatingService.batchDeleteRatings(ids);
    }

    @GetMapping("/average/{spotId}")
    public Result getAverageRating(@PathVariable Integer spotId) {
        return scenicRatingService.getAverageRating(spotId);
    }
}